import java.util.*;

public class Test22 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int V = in.nextInt();
            int n = in.nextInt();
            int[] vec=new int[n];
            for(int i=0;i<n;i++){
                vec[i] = in.nextInt();
            }

            Arrays.sort(vec);

            int sum=0;
            int right =n-1;
            while(right>=0 && sum < V){
                sum += vec[right];
                while(right>=0 && sum > V){
                    sum-=vec[right];
                }
                right--;
            }
            System.out.println(V-sum);
        }
    }






    public static void main02(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            long[] a=new long[n];
            for(int i=0;i<n;i++){
                a[i]=in.nextLong();
            }

            Arrays.sort(a);
            int right=n-1;
            int left=right-1;
            boolean b=true;
            while(left>=0){
                if(a[left] != a[right]){
                    long k=a[left];
                    while(k < a[right]){
                        k*=2;
                    }
                    if(k == a[right]){
                        break;
                    }else{
                        System.out.println("NO");
                        b=false;
                        break;
                    }
                }
                left--;
            }
            if(b){
                System.out.println("YES");
            }
        }
    }



    public static void main01(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String A=in.nextLine();
            String B=in.nextLine();

            int leftA=0,leftB=0,rightA=0,rightB=0;
            int count =0;
            while(rightB<B.length()&&leftB <= B.length()-A.length()){
                if(A.charAt(leftA) == B.charAt(leftB)){
                    rightB=leftB+1;
                    rightA++;
                    if(rightA>=A.length()){
                        break;
                    }
                    while(rightA<A.length()){
                        if(A.charAt(rightA) != B.charAt(rightB)){
                            count++;
                        }
                        rightA++;
                        rightB++;
                    }
                }
                else{
                    leftB++;
                }
            }
            System.out.println(count);
        }
    }
}
